<template>
  <div class="service-page">
    <div class="container">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1 class="page-title">服务中心</h1>
        <p class="page-subtitle">专业的客户服务，为您和爱宠提供全方位支持</p>
      </div>

      <!-- Loading状态 -->
      <div v-if="loading" class="loading-section">
        <div class="loading-spinner"></div>
        <p>正在加载服务数据...</p>
      </div>
      
      <!-- 错误状态 -->
      <div v-else-if="error" class="error-section">
        <p>⚠️ 加载失败: {{ error }}</p>
        <el-button @click="fetchServicesData" type="primary" size="small">
          重新加载
        </el-button>
      </div>

      <!-- 服务内容 -->
      <div v-else>
        <!-- 服务导航 -->
        <div class="service-nav">
          <div 
            v-for="service in serviceTypes"
            :key="service.id"
            class="service-nav-item"
            :class="{ active: activeService === service.id }"
            @click="activeService = service.id"
          >
            <el-icon><component :is="service.icon" /></el-icon>
            <span>{{ service.name }}</span>
          </div>
        </div>

      <!-- 服务内容 -->
      <div class="service-content">
        <!-- 在线客服 -->
        <div v-if="activeService === 'customer'" class="service-section">
          <h2 class="service-title">在线客服</h2>
          <div class="customer-service">
            <div class="service-info">
              <h3>多种联系方式</h3>
              <div class="contact-methods">
                <div 
                  v-for="method in customerService.contactMethods"
                  :key="method.id"
                  class="contact-method"
                >
                  <el-icon class="method-icon"><component :is="method.icon" /></el-icon>
                  <div class="method-info">
                    <h4>{{ method.title }}</h4>
                    <p>{{ method.content }}</p>
                    <span class="service-time">{{ method.detail }}</span>
                  </div>
                </div>
              </div>
            </div>
            
            <div class="service-features">
              <h3>服务特色</h3>
              <ul class="features-list">
                <li
                  v-for="(feature, index) in customerService.features"
                  :key="index"
                >
                  <el-icon><Checked /></el-icon>
                  <span>{{ feature }}</span>
                </li>
              </ul>
            </div>
          </div>
        </div>

        <!-- 常见问题 -->
        <div v-if="activeService === 'faq'" class="service-section">
          <h2 class="service-title">常见问题</h2>
          <div class="faq-categories">
            <div 
              v-for="category in faqCategories"
              :key="category.id"
              class="faq-category"
            >
              <h3 class="category-title">
                <el-icon><component :is="category.icon" /></el-icon>
                {{ category.name }}
              </h3>
              <div class="faq-list">
                <el-collapse v-model="activeFaq">
                  <el-collapse-item 
                    v-for="faq in category.questions"
                    :key="faq.id"
                    :title="faq.question"
                    :name="faq.id"
                  >
                    <div class="faq-answer">{{ faq.answer }}</div>
                  </el-collapse-item>
                </el-collapse>
              </div>
            </div>
          </div>
        </div>

        <!-- 售后服务 -->
        <div v-if="activeService === 'support'" class="service-section">
          <h2 class="service-title">售后服务</h2>
          <div class="support-services">
            <div 
              v-for="service in supportServices"
              :key="service.id"
              class="service-card card-shadow"
            >
              <div class="service-icon">
                <el-icon><component :is="service.icon" /></el-icon>
              </div>
              <h3>{{ service.title }}</h3>
              <ul>
                <li v-for="(feature, index) in service.features" :key="index">
                  {{ feature }}
                </li>
              </ul>
              <el-button type="primary" plain>{{ service.buttonText }}</el-button>
            </div>
          </div>
        </div>

        <!-- 宠物咨询 -->
        <div v-if="activeService === 'consultation'" class="service-section">
          <h2 class="service-title">宠物咨询</h2>
          <div class="consultation-services">
            <div class="consultation-intro" v-if="consultationServices.intro">
              <h3>{{ consultationServices.intro.title }}</h3>
              <p>{{ consultationServices.intro.description }}</p>
            </div>
            
            <div class="consultation-types" v-if="consultationServices.types">
              <div 
                v-for="type in consultationServices.types"
                :key="type.id"
                class="consultation-card"
              >
                <el-icon class="consultation-icon"><component :is="type.icon" /></el-icon>
                <h4>{{ type.title }}</h4>
                <p>{{ type.description }}</p>
                <ul>
                  <li v-for="(service, index) in type.services" :key="index">
                    {{ service }}
                  </li>
                </ul>
              </div>
            </div>
            
            <div class="consultation-booking" v-if="consultationServices.booking">
              <h3>{{ consultationServices.booking.title }}</h3>
              <p>{{ consultationServices.booking.description }}</p>
              <div class="booking-methods">
                <el-button 
                  v-for="method in consultationServices.booking.methods"
                  :key="method.text"
                  :type="method.type" 
                  size="large"
                >
                  {{ method.text }}
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 服务承诺 -->
      <div class="service-promise">
        <h2 class="section-title">服务承诺</h2>
        <div class="promise-grid">
          <div 
            v-for="promise in servicePromise"
            :key="promise.id"
            class="promise-item"
          >
            <el-icon class="promise-icon"><component :is="promise.icon" /></el-icon>
            <h3>{{ promise.title }}</h3>
            <p>{{ promise.description }}</p>
          </div>
        </div>
      </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { 
  Headset,
  QuestionFilled,
  Tools,
  Reading,
  Phone,
  ChatDotRound,
  Message,
  Checked,
  RefreshRight,
  StarFilled,
  House,
  Timer,
  Trophy,
  ShoppingBag,
  Van
} from '@element-plus/icons-vue'
import { servicesAPI } from '@/utils/api'

export default {
  name: 'ServicePage',
  components: {
    Headset,
    QuestionFilled,
    Tools,
    Reading,
    Phone,
    ChatDotRound,
    Message,
    Checked,
    RefreshRight,
    StarFilled,
    House,
    Timer,
    Trophy,
    ShoppingBag,
    Van
  },
  setup() {
    const activeService = ref('customer')
    const activeFaq = ref([])
    
    // 数据和加载状态
    const serviceTypes = ref([])
    const customerService = ref({})
    const faqCategories = ref([])
    const supportServices = ref([])
    const consultationServices = ref({})
    const servicePromise = ref([])
    
    const loading = ref(false)
    const error = ref(null)
    
    // 获取所有服务数据
    const fetchServicesData = async () => {
      try {
        loading.value = true
        error.value = null
        
        const response = await servicesAPI.getAll()
        
        if (response.success) {
          const data = response.data
          serviceTypes.value = data.serviceTypes || []
          customerService.value = data.customerService || {}
          faqCategories.value = data.faqCategories || []
          supportServices.value = data.supportServices || []
          consultationServices.value = data.consultationServices || {}
          servicePromise.value = data.servicePromise || []
          
          console.log('✅ 服务数据加载成功')
        } else {
          throw new Error(response.message || '获取服务数据失败')
        }
      } catch (err) {
        console.error('❌ 获取服务数据失败:', err)
        error.value = err.message
        
        // 失败时使用备用数据
        serviceTypes.value = [
          { id: 'customer', name: '在线客服', icon: 'Headset' },
          { id: 'faq', name: '常见问题', icon: 'QuestionFilled' },
          { id: 'support', name: '售后服务', icon: 'Tools' },
          { id: 'consultation', name: '宠物咨询', icon: 'Reading' }
        ]
        customerService.value = {
          contactMethods: [
            { title: '客服热线', content: '400-888-9999', detail: '服务时间：9:00-21:00' }
          ],
          features: ['专业宠物顾问团队', '快速响应服务']
        }
      } finally {
        loading.value = false
      }
    }
    
    // 页面初始化
    onMounted(() => {
      console.log('服务中心页面加载完成')
      fetchServicesData()
    })
    
    return {
      activeService,
      activeFaq,
      serviceTypes,
      customerService,
      faqCategories,
      supportServices,
      consultationServices,
      servicePromise,
      loading,
      error,
      fetchServicesData,
      // Element Plus icons
      Headset,
      QuestionFilled,
      Tools,
      Reading,
      Phone,
      ChatDotRound,
      Message,
      Checked,
      RefreshRight,
      StarFilled,
      House,
      Timer,
      Trophy,
      ShoppingBag,
      Van
    }
  }
}
</script>

<style scoped>
.service-page {
  min-height: 100vh;
  background: #f8f9fa;
  padding: 40px 0;
}

.page-header {
  text-align: center;
  margin-bottom: 50px;
  background: white;
  padding: 60px 40px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.page-title {
  font-size: 2.5rem;
  color: #333;
  margin-bottom: 15px;
}

.page-subtitle {
  font-size: 1.2rem;
  color: #666;
}

/* 服务导航 */
.service-nav {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 40px;
  flex-wrap: wrap;
}

.service-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 15px 30px;
  background: white;
  border-radius: 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  color: #666;
  font-weight: 500;
}

.service-nav-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

.service-nav-item.active {
  background: #FF8C00;
  color: white;
}

/* 服务内容 */
.service-content {
  background: white;
  padding: 50px;
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  margin-bottom: 50px;
}

.service-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
}

/* 在线客服 */
.customer-service {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 50px;
}

.contact-methods {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.contact-method {
  display: flex;
  align-items: flex-start;
  gap: 15px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
}

.method-icon {
  color: #FF8C00;
  font-size: 1.5rem;
  margin-top: 5px;
}

.method-info h4 {
  color: #333;
  margin-bottom: 8px;
}

.method-info p {
  color: #666;
  margin-bottom: 8px;
}

.service-time {
  color: #999;
  font-size: 0.9rem;
}

.features-list {
  list-style: none;
  padding: 0;
}

.features-list li {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  color: #555;
}

.features-list .el-icon {
  color: #FFB300;
  margin-right: 12px;
}

/* 常见问题 */
.faq-categories {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.category-title {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #333;
  margin-bottom: 20px;
  font-size: 1.3rem;
}

.category-title .el-icon {
  color: #FF8C00;
}

.faq-answer {
  color: #666;
  line-height: 1.6;
  padding: 10px 0;
}

/* 售后服务 */
.support-services {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 30px;
}

.service-card {
  background: white;
  padding: 30px;
  border-radius: 16px;
  text-align: center;
  transition: all 0.3s ease;
}

.service-card:hover {
  transform: translateY(-5px);
}

.service-icon {
  width: 80px;
  height: 80px;
  background: linear-gradient(45deg, #FF8C00, #FFB300);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
}

.service-icon .el-icon {
  font-size: 2rem;
  color: white;
}

.service-card h3 {
  color: #333;
  margin-bottom: 20px;
  font-size: 1.3rem;
}

.service-card ul {
  list-style: none;
  padding: 0;
  margin-bottom: 25px;
  text-align: left;
}

.service-card li {
  color: #666;
  margin-bottom: 8px;
  padding-left: 15px;
  position: relative;
}

.service-card li::before {
  content: '•';
  color: #FF8C00;
  position: absolute;
  left: 0;
}

/* 宠物咨询 */
.consultation-services {
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.consultation-intro {
  text-align: center;
}

.consultation-intro h3 {
  color: #333;
  margin-bottom: 15px;
  font-size: 1.5rem;
}

.consultation-intro p {
  color: #666;
  font-size: 1.1rem;
  line-height: 1.6;
}

.consultation-types {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 30px;
}

.consultation-card {
  background: #f8f9fa;
  padding: 30px;
  border-radius: 12px;
  text-align: center;
}

.consultation-icon {
  color: #FF8C00;
  font-size: 2.5rem;
  margin-bottom: 20px;
}

.consultation-card h4 {
  color: #333;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.consultation-card > p {
  color: #666;
  margin-bottom: 20px;
  line-height: 1.6;
}

.consultation-card ul {
  list-style: none;
  padding: 0;
  text-align: left;
}

.consultation-card li {
  color: #555;
  margin-bottom: 8px;
  padding-left: 15px;
  position: relative;
}

.consultation-card li::before {
  content: '✓';
  color: #FFB300;
  position: absolute;
  left: 0;
}

.consultation-booking {
  text-align: center;
  background: #f8f9fa;
  padding: 40px;
  border-radius: 12px;
}

.consultation-booking h3 {
  color: #333;
  margin-bottom: 15px;
}

.consultation-booking p {
  color: #666;
  margin-bottom: 25px;
}

.booking-methods {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

/* 服务承诺 */
.service-promise {
  background: white;
  padding: 50px;
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.section-title {
  font-size: 2rem;
  text-align: center;
  color: #333;
  margin-bottom: 40px;
}

.promise-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 30px;
}

.promise-item {
  text-align: center;
  padding: 30px 20px;
}

.promise-icon {
  color: #FF8C00;
  font-size: 2.5rem;
  margin-bottom: 20px;
}

.promise-item h3 {
  color: #333;
  margin-bottom: 10px;
  font-size: 1.2rem;
}

.promise-item p {
  color: #666;
  line-height: 1.6;
}

/* 加载和错误状态样式 */
.loading-section {
  text-align: center;
  padding: 80px 20px;
  color: #666;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #FF8C00;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-section p {
  margin: 0;
  font-size: 1.1rem;
}

.error-section {
  text-align: center;
  padding: 80px 20px;
  color: #ff4757;
}

.error-section p {
  margin: 0 0 20px 0;
  font-size: 1.1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .service-page {
    padding: 20px 0;
  }
  
  .page-header {
    padding: 40px 20px;
    margin-bottom: 30px;
  }
  
  .page-title {
    font-size: 2rem;
  }
  
  .service-nav {
    gap: 10px;
  }
  
  .service-nav-item {
    padding: 12px 20px;
    font-size: 0.9rem;
  }
  
  .service-content,
  .service-promise {
    padding: 30px 20px;
  }
  
  .customer-service {
    grid-template-columns: 1fr;
    gap: 30px;
  }
  
  .support-services,
  .consultation-types {
    grid-template-columns: 1fr;
  }
  
  .booking-methods {
    flex-direction: column;
    align-items: center;
  }
  
  .booking-methods .el-button {
    width: 200px;
  }
  
  .promise-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 20px;
  }
}
</style>
